﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.web;
using System.Xml;

namespace AD_Notifications_Dashboard.automation
{
    public class AppBase : umbraco.BusinessLogic.ApplicationBase
    {

        public AppBase()
        {
            Document.AfterSendToPublish += new Document.SendToPublishEventHandler(Document_AfterSendToPublish);  
        }

        void Document_AfterSendToPublish(Document sender, SendToPublishEventArgs e)
        {
            
           
            //User u = User.GetCurrent(); 
            //Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is AfterSendToPublish by " + u.Id + " by " + sender.ReleaseDate + " by ");
            CreateXML(sender);


        }


        #region Create XML
        public void CreateXML(Document sender)
        {
            try
            {
        
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("../data/AD_Notifications_Dashboard.xml"));
            //host
            XmlElement subRoot = doc.CreateElement("node");
            
            //ID
            XmlElement ElId = doc.CreateElement("id");
            XmlText TxtId = doc.CreateTextNode(sender.Id.ToString());
            ElId.AppendChild(TxtId);
            subRoot.AppendChild(ElId);

            //Name
            XmlElement ElName = doc.CreateElement("name");
            XmlText TxtName = doc.CreateTextNode(sender.Text);
            ElName.AppendChild(TxtName);
            subRoot.AppendChild(ElName);

               
            //Version sender.Version
            XmlElement ElVer = doc.CreateElement("version");
            XmlText TxtVer = doc.CreateTextNode(sender.Version.ToString());
            ElVer.AppendChild(TxtVer);
            subRoot.AppendChild(ElVer);


            //Date
            XmlElement ElDate = doc.CreateElement("date");
            XmlText TxtDate = doc.CreateTextNode(XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Utc));
                
            ElDate.AppendChild(TxtDate);
            subRoot.AppendChild(ElDate);
            
            //User
            User u = User.GetCurrent();
            XmlElement ElUser = doc.CreateElement("user");
            XmlText TxtUser = doc.CreateTextNode(u.LoginName);
            XmlAttribute AttrUser = doc.CreateAttribute("id");
            AttrUser.Value = u.Id.ToString();
            ElUser.SetAttributeNode(AttrUser);
            ElUser.AppendChild(TxtUser);
            subRoot.AppendChild(ElUser);
            
            XmlElement parent = (XmlElement)doc.SelectSingleNode("/data");
            // Try to remove already existing notification on same id. 
            XmlNode removeNode = parent.SelectSingleNode("/data/node[id = " + sender.Id + " ]");
            if (removeNode != null)
            {
                parent.RemoveChild(removeNode);

            }

            parent.AppendChild(subRoot);

            doc.Save(HttpContext.Current.Server.MapPath("../data/AD_Notifications_Dashboard.xml"));


            }
            catch (Exception err)
            {
                Log.Add(LogTypes.Error, sender.Id, err.StackTrace);
                // do nothing
            }
      

        }

        #endregion
    }

}
